package com.eeepay.eeepay_v2.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ScrollView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.activity.WebViewActivity;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.v2_library.pulltorefresh.PullToRefreshBase;
import com.eeepay.v2_library.pulltorefresh.PullToRefreshScrollView;
import com.eeepay.v2_library.util.ScreenSwitch;
import com.eeepay.v2_library.view.TitleBar;

/**
 * 描述：资讯模块
 * 作者：zhuangzeqin
 * 时间: 2017/9/26-17:56
 * 邮箱：zzq@eeepay.cn
 */
public class InformationFragment extends ABBaseFragment {
    private WebView web_infor;//加载
    private PullToRefreshScrollView scrollView;
    /**
     * 获得创建实例
     * @return
     */
    public static InformationFragment getInstance()
    {
        InformationFragment informationFragment = new InformationFragment();
        return informationFragment;
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_information, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        scrollView = (PullToRefreshScrollView) view.findViewById(R.id.pull_refresh_scroll_view);
        TitleBar titleBar = (TitleBar)view.findViewById(R.id.title_bar);
        titleBar.setShowLeft(View.GONE);
        titleBar.setTiteTextView(getResources().getString(R.string.merchant_information));
        titleBar.setBackgroundColor(Color.parseColor("#FDFDFD"));
        inintWebSetting(view);
        initListen();
    }

    /**
     * 监听事件的编写
     */
    private void initListen()
    {
        //下拉刷新
        scrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                web_infor.reload();//重新加载
            }
        });
    }

    /**
     * 初始化websetting
     */
    private void inintWebSetting(View view) {
        web_infor = (WebView)view.findViewById(R.id.web_infor);
        /** 提高渲染的优先级**/
        web_infor.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        web_infor.getSettings().setDefaultTextEncodingName("utf-8");//设置编码格式
        /** 设置缓存模式**/
        web_infor.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//根据cache-control决定是否从网络上取数据。
        /** 设置webview 字体大小**/
        web_infor.getSettings().setTextSize(WebSettings.TextSize.NORMAL);
        /** 设置H5的缓存是否打开，默认关闭**/
        web_infor.getSettings().setAppCacheEnabled(false);
        //支持javascript
        web_infor.getSettings().setJavaScriptEnabled(true);
        //自适应屏幕
        web_infor.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        web_infor.getSettings().setLoadWithOverviewMode(true);
        // 加上这句话，支持html5的某些alert提示框的弹出
        web_infor.getSettings().setDomStorageEnabled(true);
        /**
         * 事件监听对象(WebViewClient) 处理各种通知、拦截请求事件的
         */
        web_infor.setWebViewClient(new WebViewClientObject());
        web_infor.loadUrl(Constant.URL.INFORMATIONURL);
    }

    /**
     * 事件监听对象(WebViewClient) 处理各种通知、拦截请求事件的，
     */
    protected class WebViewClientObject extends WebViewClient {
        /**
         * 对网页中超链接按钮的响应
         **/
        @Override
        public boolean shouldOverrideUrlLoading(final WebView webView, String url) {
            Bundle bundle = new Bundle();
            bundle.putString(Constant.TITLE,  webView.getTitle());
            bundle.putString(Constant.CANPS_QUERY, url);
            bundle.putString(Constant.INTENT_FLAG, Constant.CANPS_QUERY);
            ScreenSwitch.switchActivity(mContext, WebViewActivity.class, bundle,-1);
//            web_infor.loadUrl(url);
            return true;
        }
        /**
         * 载入页面开始的事件
         **/
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            showProgressDialog();
            super.onPageStarted(view, url, favicon);
        }
        /**
         * 载入页面完成的事件
         **/
        @Override
        public void onPageFinished(WebView webView, String url) {
            dismissProgressDialog();
            if (scrollView.isRefreshing()) {
                scrollView.onRefreshComplete();
            }
            super.onPageFinished(webView, url);
        }
        @Override
        public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
            sslErrorHandler.proceed();// 接受证书
            super.onReceivedSslError(webView, sslErrorHandler, sslError);
        }


    }

}

